package com.comtrade.map;


import android.animation.Animator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;

import com.example.beaconapk.R;

public class DotView extends View{
	
	private float xCoor, yCoor;
	private int h,w;
	
	private float oldX, oldY;
	private long animationDuration;
	
	private View parentView;
	
	private Drawable logo;
	
	private ValueAnimator animator;
	
	public DotView(Context context, View view){
		super(context);
		
		parentView = view;
		animator = new ValueAnimator();
		Bitmap slika = BitmapFactory.decodeResource(getResources(), R.drawable.ct_logo);
		logo = new BitmapDrawable(getResources(), slika);	
		h=30; w=30;
		logo.setBounds(0,0,h,w);	
		
		xCoor = 0.0f;
		yCoor = 0.0f;
		
		animationDuration = 500;
	}

	public DotView(Context context){
        super(context);
        
	}
	/**
	 * draws current position
	 */
	@Override
	protected void onDraw(Canvas canvas){			
		super.onDraw(canvas);		
		
		canvas.save();
		
		canvas.translate(xCoor - w/2, yCoor - h/2);		
		//canvas.drawBitmap(((BitmapDrawable)logo).getBitmap(), 0, 0, null);
		logo.draw(canvas);
		canvas.restore();
		
		parentView.invalidate();
	}
	
	public int getH() {
		return h;
	}
	
	public void setH(int h) {
		this.h = h;
	}
	
	public int getW() {
		return w;
	}
	
	public void setW(int w) {
		this.w = w;
	}
	
	public void setAnimationDuration(long d) {
		animationDuration = d;
	}
		
	public float getxCoor() {
		return xCoor;
	}

	public void setxCoor(float xCoor) {
		this.xCoor = xCoor;
	}

	public float getyCoor() {
		return yCoor;
	}

	public void setyCoor(float yCoor) {
		this.yCoor = yCoor;
	}
	
	private void doAnimation(float x, float y){

	}
	/**
	 * animating method which lasts for animationDuration
	 * from current position to (x,y). 
	 * @param x
	 * @param y
	 */
	
	private float dx;
	private float dy;
	
	public void setCoordinates(final float x, final float y){		
		oldX = xCoor;
		oldY = yCoor;
		dx = x-xCoor;
		dy = y-yCoor;

		animator = ValueAnimator.ofFloat(0,1.0f);
		
		animator.setInterpolator(null);
		animator.setDuration(animationDuration);
		
		animator.addUpdateListener(new AnimatorUpdateListener() {
						
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				
				Log.d("ANIM", "4");				
				xCoor = oldX+dx * (Float)animation.getAnimatedValue();
				yCoor = oldY+dy * (Float)animation.getAnimatedValue();
				Log.d("ANIM", "5");
				//invalidate();
				parentView.invalidate();
				Log.d("ANIM", "6");
			}
		});

				  
				animator.start();

	}
}
